﻿using System;
using System.ComponentModel;
using System.Linq.Expressions;

namespace PomodoroMinutes.MVVMBase
{
	public class ViewModelBase : INotifyPropertyChanged
	{
		public event PropertyChangedEventHandler PropertyChanged;

		public void NotifyPropertyChanged(Expression<Func<object>> propertyExpression)
		{
			if (PropertyChanged == null) return;
			PropertyChanged(this, new PropertyChangedEventArgs(GetPropertyName(propertyExpression)));
		}

		private static string GetPropertyName(Expression<Func<object>> propertyExpression)
		{
			var unaryExpression = propertyExpression.Body as UnaryExpression;
			var memberExpression = unaryExpression == null ? (MemberExpression)propertyExpression.Body : (MemberExpression)unaryExpression.Operand;
			var propertyName = memberExpression.Member.Name;
			return propertyName;
		}
	}
}